package cn.aihongbo.kmp.pages.home

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.PageSize
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.CircularProgressIndicator
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.unit.dp
import cn.aihongbo.kmp.data.bean.HbcConfigBannerItemBean
import cn.aihongbo.kmp.data.vm.HomeViewModel
import coil3.compose.AsyncImage

@Composable
fun HomeTopBannerView(viewModel: HomeViewModel) {

    val pagerState = rememberPagerState { viewModel.banners.size }

    Box(modifier = Modifier.fillMaxWidth()) {
        if (viewModel.isLoading.value) {
            CircularProgressIndicator(
                modifier = Modifier.align(Alignment.Center).padding(top = 60.dp)
            )
        }
        HorizontalPager(
            contentPadding = PaddingValues(0.dp), pageSpacing = 16.dp,
            state = pagerState,
            modifier = Modifier,
            pageSize = PageSize.Fill,
            userScrollEnabled = true,
        ) { page ->
            HomeTopBannerItem(page, viewModel.banners[page])
        }
    }
}

@Composable
fun HomeTopBannerItem(page: Int, bean: HbcConfigBannerItemBean) {
    AsyncImage(
        model = bean.bannerUrl,
        contentDescription = bean.keywordGroupDescription,
        modifier = Modifier.clip(RoundedCornerShape(bottomStart = 16.dp, bottomEnd = 16.dp))
    )
}